/**
 * 海报
 * */
app.controller('ads', ['$scope', function ($scope) {
    var analogData = [
	{
		href: 'task-center.html',
		src: 'images/entrance/banner_1.png',
	},
	{
        href: 'site.html',
        src: 'images/entrance/banner_2.gif',
    }];
    $scope.pics = analogData;
    setTimeout(function () {
        $(".slideBox-en").hover(function () {
            $(".prev,.next").show();
        }, function () {
            $(".prev,.next").hide();
        }).slide({mainCell: ".bd ul",effect:"fold", autoPlay: true});
    }, 0);

}]);

/**
 * 资讯
 */
app.controller('publicAnnouncement', ['$scope', function ($scope) {
   var publicData = [];
   var newsData = [];
   
   AQ.callAQJSON("getIndexPublicForum.do", null, function(result) {
		var list = result.data;
		for (var i=0; i<7; i++) {
			var forumEntry = list[i];
			publicData.push(forumEntry);
		}
		$scope.$apply(function () {
			$scope.public = publicData;
		});
	}, function(err, code) {
		console.log(err + "["+ code +"]");
	});
	
	var param = {
		cateIds: null,
		pageNo: 1,
		pageCount: 7,
	};
	AQ.callAQJSON("getNewEntry.do", param, function(result) {
		var list = result.data;
		var data = [];
		for (var i=0; i<list.length; i++) {
			var forumEntry = list[i];
			forumEntry.categoryId = DIC.ForumCate(forumEntry.categoryId);
			newsData.push(forumEntry);
		}
		$scope.$apply(function () {
			$scope.news = newsData;
		});
	}, function(err, code) {
		console.log(err + "["+ code +"]");
	});
	
}]);

/**
 * 下方小广告
 * */
app.controller('adsSmall', ['$scope', function ($scope) {
    var analogData = [{
        href: 'publish.html',
        title:'中小卖家如何做好淘宝',
        src: 'images/entrance/banner_1.png'
    },{
        href: 'task-center.html',
        title:'让你接单接到手软',
        src: 'images/entrance/banner_1.png'
    }];
    $scope.pics = analogData;
    setTimeout(function () {
        $(".slideBox2").slide({mainCell: ".bd ul",effect:"fold", autoPlay: true});
    }, 0);

}]);



/* 刷新页面时重置表单 */
//window.onload = function() {
//    $("form").each(function(index, element) {
//       element.reset();
//    });
//}

app.controller('loginCtrl', ['$scope', function ($scope) {
	$scope.userKey = $.cookie("userKey");
	
	$scope.isLogined = 0;
	if($.cookie("isLogined") == 1) {
		$scope.isLogined = 1;
		AQ.callAQ("refreshUserInfo.do", null, function(result) {
			var User = result[1];
			$.cookie("userName", User.userName, {expires: 30});
			$.cookie("User", JSON.stringify(User));
			
			User.vipText = User.vip >= 1 ? "VIP会员" : "普通用户";
			$scope.$apply(function () {
				$scope.User = User;
			});
		}, function(err, code){
			console.log(err + "["+ code +"]");
		});
	}
	
    $scope.login = function () {
        var userName = $.trim($("#userName").val());
        var password = $.trim($("#password").val());
		var keyType = 1;
		
		var numReg = /^[0-9]+$/;
		var phoneReg = /^[1][34578][0-9]{9}$/;
		var mailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		var nameReg = /^[\u4E00-\u9FA50-9A-Za-z_-]{3,25}$/;
		var pwdReg = /^[0-9A-Za-z!@#$%&*_-]{6,25}$/;
		if(numReg.test(userName)) {
			keyType = 2;
			if (!phoneReg.test(userName)) {
            	$("#error-msg-tips").text("手机号码格式不正确！");
            	return;
        	}
		} else if(userName.indexOf('@') >= 0) {
			keyType = 3;
			if(!mailReg.test(userName)) {
            	$("#error-msg-tips").text("邮箱格式不正确！");
				return;
			}
		} else {
			keyType = 1;
			if(!nameReg.test(userName)) {
            	$("#error-msg-tips").text("用户名格式不正确！");
				return;
			} else if(Utils.countLength(userName) < 5 || Utils.countLength(userName) > 25) {
            	$("#error-msg-tips").text("用户名长度不合法！");
				return;
			}
		}
		if(!pwdReg.test(password)) {
            $("#error-msg-tips").text("密码格式不正确！");
			return;
		}
		$("#submit-login").prop("disabled", true);
		$("#submit-login").val("正在登录···");
		
        var param = {
            userKey: userName,
            passWord: password,
            keyType: keyType,
        };
        AQ.callAQ("userLogin.do", param, function(result) {
            $scope.$apply(function(){
                $.cookie("userKey", userName, {expires: 30});
				$.cookie("userName", result[1].userName, {expires: 30});
				$.cookie("User", JSON.stringify(result[1]));
				$.cookie("isLogined", 1);
				if(result[1].experience < 30) {
					$.cookie("firstLogin", true);
				}
            });
            var redirectURL = Utils.getUrlParam("redirectURL");
            if(redirectURL != null) {
				 location.replace(redirectURL);
			} else {
				 location.replace("task-center.html");
			}
        }, function(err, code) {
			$("#error-msg-p").show();
            $("#error-msg-tips").text(err);
			$("#submit-login").val("登录");
			$("#submit-login").prop("disabled", false);
        });
    };
	
	
}]);
